/**
 * 自定义函数
 * */


//列表
function getIndex(){
    typeTitle();
    var formData = {
        type: type,
        pageStart: pages.start,
        pageLimit: pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.fuwuIndex.method,
        url: apis.fuwuIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i, id;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].type_name + '</td>';
        h += '<td>';
        if (d[i].thumb == '') {
            h += '-';
        } else {
            h += '<img src="/' + d[i].thumb + '" height="100"/>';
        }
        h += '</td>';
        h += '<td>';
        h += d[i].price;
        h += '<span style=";color: #ccc; text-decoration: line-through;">' + d[i].gsprice + '</span>';
        h += '</td>';
        h += '<td style="text-align: left;">' + d[i].cnt + '</td>';
        h += '<td>' + d[i].lasttime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'fuwu-edit.html\', ' + id + ');">';
        h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" style="color: red;" onclick="lay_del(' + id + ');">';
        h += '删除';
        h += '</a>';
        h += '<input type="hidden" id="title' + id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="type' + id + '" value="' + d[i].type + '"/>';
        h += '<input type="hidden" id="price' + id + '" value="' + d[i].price + '"/>';
        h += '<input type="hidden" id="gsprice' + id + '" value="' + d[i].gsprice + '"/>';
        h += '<input type="hidden" id="thumb' + id + '" value="' + d[i].thumb + '"/>';
        h += '<input type="hidden" id="cnt' + id + '" value="' + d[i].cnt + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//当前分类
function typeTitle(){
    var typeDiv = $('#typeTitle');
    if (type == 1) {
        typeDiv.html('家政蒸洗');
    }
    if (type == 2) {
        typeDiv.html('网页小程序');
    }
    if (type == 3) {
        typeDiv.html('剪辑合成');
    }
}


//编辑
function lay_add(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//编辑
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var type = $('#type' + id).val();
    var price = $('#price' + id).val();
    var gsprice = $('#gsprice' + id).val();
    var thumb = $('#thumb' + id).val();
    var cnt = $('#cnt' + id).val();
    url += '&title=' + escape(tt);
    url += '&type=' + type;
    url += '&price=' + price;
    url += '&gsprice=' + gsprice;
    url += '&thumb=' + escape(thumb);
    url += '&cnt=' + escape(cnt);
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    var h = '';
    $('#title')[0].value = title;
    if (type != '') {
        h = '<option value="0">-选择-</option>';
        if (type == 1) {
            h += '<option value="1" selected>家政蒸洗</option>';
        } else {
            h += '<option value="1">家政蒸洗</option>';
        }
        if (type ==2) {
            h += '<option value="2" selected>网页小程序</option>';
        } else {
            h += '<option value="2">网页小程序</option>';
        }
        if (type == 3) {
            h += '<option value="3" selected>剪辑合成</option>';
        } else {
            h += '<option value="3">剪辑合成</option>';
        }
        $('#type').html(h);
    }
    $('#price')[0].value = price;
    $('#gsprice')[0].value = gsprice;
    $('#cnt').html(cnt);
    if (thumb != '') {
        h = '<img src="/' + thumb + '"/>';
        h += '<input type="hidden" id="img" value="' + thumb + '"/>';
        $('#bigShow').html(h);
    }
}


////抓图
//function uploadImg() {
//    var fileObj = $('#bigUpload')[0], reader = new FileReader();
//    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
//    reader.onload = function (res){
//        //showImg({img: res.target.result});
//        var formData = {
//            id: id, img: res.target.result,
//        };
//        $.ajax({
//            type: apis.tmp1UploadImg.method,
//            url: apis.tmp1UploadImg.url,
//            data: formData,
//            success: function (res) {
//                console.log(res);
//                if (res.code != 0) {
//                    msgPop({msg: res.msg}); return
//                }
//                msgPop({msg: '操作成功'});
//                setTimeout(function () {
//                    location.reload();
//                }, 1000);
//            },
//            error: function () {
//                console.log('网络错误');
//            }
//        });
//    }
//}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//更新物品
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        type: $('#type').val(),
        price: $('#price').val(),
        gsprice: $('#gsprice').val(),
        cnt: $('#cnt').val(),
        thumb: $('#img').val(),
    };
    if (!formData.title) {
        msgPop({msg: '标题必须的'}); return
    }
    if (!formData.type) {
        msgPop({msg: '分类必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.fuwuModify.method,
        url: apis.fuwuModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function () {
                //location.reload();
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//更新物品
function lay_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.fuwuDel.method,
            url: apis.fuwuDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code != 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //msgPop({msg: '删除成功'});
                layerMsgOk('删除成功');
                setTimeout(function () {
                    //location.reload();
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('网络错误');
            }
        });
    });
}


//日期列表
function jzday(){
    var formData = {
        pageStart: start,
        pageLimit: pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.fuwuJzday.method,
        url: apis.fuwuJzday.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(jzdayHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function jzdayHtml(d){
    var h = '', i, id;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" onclick="lay_img(\'详情\',\'fuwu-jzimg.html\', ' + id + ');">';
        h += '详情';
        h += '</a>';
        h += '<input type="hidden" id="day' + id + '" value="' + d[i].title + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//图片集合
function lay_img(title,url,id,w,h){
    url += '?id=' + id;
    var day = $('#day' + id).val();
    url += '&day=' + escape(day);
    layer_show(title,url,w,h);
}


//图片列表
function jzimg(){
    var formData = {
        day: day,
        pageStart: start,
        pageLimit: pages.limit,
    };
    console.log(formData);
    $.ajax({
        type: apis.fuwuJzimg.method,
        url: apis.fuwuJzimg.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(jzimgHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function jzimgHtml(d){
    var h = '', i, id;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td><a href="/' + d[i].img + '" target="_blank"><img src="/' + d[i].img + '" width="100"/></a></td>';
        h += '<td>' + d[i].addtime + '</td>';
        //h += '<td class="td-manage">';
        //h += '<a href="javascript:;" onclick="lay_img(\'详情\',\'fuwu-jzimg.html\', ' + id + ');">';
        //h += '详情';
        //h += '</a>';
        //h += '<input type="hidden" id="day' + id + '" value="' + d[i].title + '"/>';
        //h += '</td>';
        h += '</tr>';
    }
    return h;
}